package com.android.videotest.music.ui.musiclist;

import android.graphics.Color;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaDescriptionCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.videotest.R;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：VideoTest
 * 类 名 称：MusicListAdapter
 * 类 描 述：MusicListAdapter
 * 创建时间：2019-11-18 10:34
 * 创 建 人：sansecy
 */
public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.SongHolder> {
//    private int mSelectedPosition = -1;

    private List<MediaBrowserCompat.MediaItem> mMediaItems;
    private String mMediaId;

    public void setMediaItems(List<MediaBrowserCompat.MediaItem> mediaItems) {
        mMediaItems = mediaItems;
        notifyDataSetChanged();
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public SongHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_music_list, parent, false);
        return new SongHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SongHolder holder, int position) {
        MediaBrowserCompat.MediaItem item = mMediaItems.get(position);
        MediaDescriptionCompat description = item.getDescription();
        holder.tvTitle.setText(description.getTitle());
        holder.tvAlbum.setText(description.getDescription());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onClick(item, holder.getAdapterPosition());
                }
//                mSelectedPosition = holder.getAdapterPosition();
//                notifyDataSetChanged();
            }
        });
        if (Objects.equals(item.getMediaId(), mMediaId)) {
            holder.itemView.setBackgroundColor(Color.LTGRAY);
        } else {
            holder.itemView.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    @Override
    public int getItemCount() {
        return mMediaItems == null ? 0 : mMediaItems.size();
    }

    public void setPlayingMediaId(String mediaId) {
        mMediaId = mediaId;
        notifyDataSetChanged();
    }

    static class SongHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_album)
        TextView tvAlbum;

        public SongHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

   public interface OnItemClickListener {
        void onClick(MediaBrowserCompat.MediaItem item, int position);
    }

}

